//
//  OfficialMediaViewController.m
//  AnXin100
//
//  Created by 王合润 on 10/17/18.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "OfficialMediaViewController.h"
#import "SDCycleScrollView.h"
#import "BaseShowViewController.h"
#import "CooperationCaseViewController.h"
#import "InBrandViewController.h"
#import "AboutUsViewController.h"
#import "EnterpriseDynamicViewController.h"
#import "InteractSecondMenuModel.h"
#import "UIButton+WebCache.h"
#import "NewsModel.h"
#import "NewsWebViewController.h"
@interface OfficialMediaViewController ()<SDCycleScrollViewDelegate>

@property (nonatomic, strong) SDCycleScrollView *headImageView;   // 顶部轮播图

@property (nonatomic, strong) SDCycleScrollView *newsImageView;   // 新闻页

@property (nonatomic, strong) UIView *menuView;

@property (nonatomic, strong) UIView *newsView;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) UILabel *newsTitleLabel;

@property (nonatomic, strong) NSDictionary *headImagesDataDic;

@property (nonatomic, strong) NSDictionary *newsImageDataDic;

@property (nonatomic, strong) NSMutableArray *menuDataAry;

@property (nonatomic, strong) NSMutableArray *newsAry;

@end

@implementation OfficialMediaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [PublicMonMethods setupNavigationItems:self];
    [PublicMonMethods settitle:@"官方自媒体" viewController:self];
    [self.view addSubview:self.mainScrollView];
    [self getMenuData];
    [self getPhotoDatas];
}

#pragma mark - lazy load
- (UIScrollView *)mainScrollView {
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [_mainScrollView addSubview:self.headImageView];
        [_mainScrollView addSubview:self.menuView];
        [_mainScrollView addSubview:self.newsTitleLabel];
        [_mainScrollView addSubview:self.newsImageView];
        [_mainScrollView addSubview:self.newsView];
        _mainScrollView.showsVerticalScrollIndicator = NO;
    }
    return _mainScrollView;
}

#pragma mark - lazy load
- (SDCycleScrollView *)headImageView {
    if (!_headImageView) {
        _headImageView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 180) shouldInfiniteLoop:YES imageNamesGroup:nil];
        _headImageView.delegate = self;
        _headImageView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _headImageView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    }
    return _headImageView;
}

- (UIView *)menuView {
    if (!_menuView) {
        _menuView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_headImageView.frame), SCREEN_WIDTH, 205)];
    }
    return _menuView;
}
 
- (UILabel *)newsTitleLabel {
    if (!_newsTitleLabel) {
        _newsTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_menuView.frame), SCREEN_WIDTH, 15)];
        _newsTitleLabel.text = @"精选头条";
        _newsTitleLabel.font = FONT_SIXTEEN;
        _newsTitleLabel.textColor = [UIColor colorWithHex:0x29B566];
    }
    return _newsTitleLabel;
}

- (SDCycleScrollView *)newsImageView {
    if (!_newsImageView) {
        
        NSArray *imageNames = @[@"m1.jpeg"];// 本地图片请填写全名
        _newsImageView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, CGRectGetMaxY(_newsTitleLabel.frame) + 20, SCREEN_WIDTH, 180) shouldInfiniteLoop:YES imageNamesGroup:imageNames];
        _newsImageView.delegate = self;
        _newsImageView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        _newsImageView.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    }
    return _newsImageView;
}

- (UIView *)newsView {
    if (!_newsView) {
        _newsView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_newsImageView.frame), SCREEN_WIDTH, 300)];
    }
    return _newsView;
}

- (void)setMenuViewUI {
    NSArray *menuDataSourceAry = @[@{@"title" : @"基地展示",
                                     @"icon" : @"base"},
                                   @{@"title" : @"合作案例",
                                     @"icon" : @"enterpriseDynamic"},
                                   @{@"title" : @"入驻品牌",
                                     @"icon" : @"policyMessage"},
                                   @{@"title" : @"关于我们",
                                     @"icon" : @"industryNews"},
                                   @{@"title" : @"企业动态",
                                     @"icon" : @"InnovationColumns"},
                                   @{@"title" : @"政策信息",
                                     @"icon" : @"policyMessage"},
                                   @{@"title" : @"行业新闻",
                                     @"icon" : @"industryNews"},
                                   @{@"title" : @"创新栏目",
                                     @"icon" : @"InnovationColumns"}];
    for (NSInteger i = 0; i < _menuDataAry.count; i++) {
        InteractSecondMenuModel *model = _menuDataAry[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(SCREEN_WIDTH / 4 * i, 20, SCREEN_WIDTH / 4, 90);
        [btn setTitle:model.demoSecondName forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHex:0x333333] forState:UIControlStateNormal];
        btn.titleLabel.font = FONT_FOURTEEN;
        [btn setImage:[UIImage imageNamed:menuDataSourceAry[i][@"icon"]] forState:UIControlStateNormal];
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [btn setButtonLayout:CHButtonLayoutTitleBottom offset:-10];
        [_menuView addSubview:btn];
        if (i > 3) {
            btn.mj_x = SCREEN_WIDTH / 4 * (i - 4);
            btn.mj_y = 110;
        }
    }
}

- (void)setNewsViewUI {
    NSArray *newsAry = @[@{@"title" : @"祥云“四个注重”加快生态农业发展",
                           @"date" : @"2018.9.9"},
                         @{@"title" : @"祥云“四个注重”加快生态农业发展",
                           @"date" : @"2018.9.9"},
                         @{@"title" : @"祥云“四个注重”加快生态农业发展",
                           @"date" : @"2018.9.9"},
                         @{@"title" : @"祥云“四个注重”加快生态农业发展",
                           @"date" : @"2018.9.9"}];
    
    for (NSInteger i = 0; i < _newsAry.count; i++) {
        
        NewsModel *model = _newsAry[i];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 35 * i, SCREEN_WIDTH, 35)];
        [_newsView addSubview:view];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 10, SCREEN_WIDTH / 3 * 2 - 10, 15)];
        titleLabel.text = model.axNewTip;
        titleLabel.textColor = [UIColor colorWithHex:0x333333];
        titleLabel.font = FONT_FOURTEEN;
        [view addSubview:titleLabel];
        
        UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), 10, SCREEN_WIDTH / 3 - 10, 15)];
        dateLabel.text = model.publishTime;
        dateLabel.textColor = [UIColor colorWithHex:0x333333];
        dateLabel.font = FONT_FOURTEEN;
        dateLabel.textAlignment = NSTextAlignmentRight;
        [view addSubview:dateLabel];

    }
    _newsView.mj_h = 35 * _newsAry.count;
    _mainScrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(_newsView.frame));

}


#pragma mark - Action
- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)btnClickAction:(UIButton *)sender {
    if (sender.tag == 100) {
        
        InteractSecondMenuModel *model = _menuDataAry[sender.tag - 100];
        [NetWorkHelp yl_postDataWith:@{@"demoSecondId" : model.demoSecondId, @"pageSize" : @"4", @"pageNum" : @"1"} url:URL_SPLIT(URL_Interactive, URL_Interactive_News) success:^(id respose) {
            
            [NetWorkHelp yl_postDataWith:@{@"newid" : respose[@"body"][@"news"][0][@"newId"]} url:URL_SPLIT(URL_Interactive, URL_Interactive_NewsDetail) success:^(id respose) {
                
                NewsWebViewController *vc = [NewsWebViewController new];
                vc.urlString = respose[@"body"][@"newAddr"];
                [self.navigationController pushViewController:vc animated:YES];
                
            } failure:^(NSError *error) {
                
            }];

        } failure:^(NSError *error) {
            
        }];
    }
    if (sender.tag == 101) {
        
        
        InteractSecondMenuModel *model = _menuDataAry[sender.tag - 100];
        EnterpriseDynamicViewController *vc = [EnterpriseDynamicViewController new];
        vc.url_Id = model.demoSecondId;
        [self.navigationController pushViewController:vc animated:YES];

    }
    
    if (sender.tag == 102) {
        InBrandViewController *vc = [InBrandViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (sender.tag == 103) {
        AboutUsViewController *vc = [AboutUsViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (sender.tag == 104) {

    }
    
}


// 获取菜单栏目
- (void)getMenuData {
    
    ZX_WEAK_SELF
    [NetWorkHelp yl_postDataWith:[NSMutableDictionary dictionaryWithDictionary:@{@"demoid" : _demoId}] url: URL_SPLIT(URL_Interactive, URL_Interactive_QuerySecondMenu) success:^(id respose) {
        
        // 布局菜单部分
        weakSelf.menuDataAry = [InteractSecondMenuModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"baseDemoSeconds"]];
        [self setMenuViewUI];
        
        // 请求下方新闻列表
        NSString *newsId = respose[@"body"][@"baseDemoSeconds"][4][@"demoSecondId"];
        [NetWorkHelp yl_postDataWith:[NSMutableDictionary dictionaryWithDictionary:@{@"demoSecondId" : newsId, @"pageSize" : @"4", @"pageNum" : @"1"}] url:URL_SPLIT(URL_Interactive, URL_Interactive_News) success:^(id respose) {
            NSLog(@"%@", respose);
            // 布局底部新闻列表
            weakSelf.newsAry = [NewsModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"news"]];
            [self setNewsViewUI];

        } failure:^(NSError *error) {
            
        }];
        
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)getPhotoDatas {
    
    ZX_WEAK_SELF
    [NetWorkHelp yl_postDataWith:[NSMutableDictionary dictionaryWithDictionary:@{@"location" : @"gfzmt1"}] url:URL_SPLIT(URL_Interactive, URL_Interactive_Viedo) success:^(id respose) {
        NSLog(@"%@", respose);
        weakSelf.headImagesDataDic = respose[@"body"][@"picOrVideo"];
        NSMutableArray *imagesAry = [NSMutableArray array];
        for (NSDictionary *dic in weakSelf.headImagesDataDic) {
            [imagesAry addObject:dic[@"fileUrl"]];
        }
        weakSelf.headImageView.imageURLStringsGroup = imagesAry;
        
    } failure:^(NSError *error) {
        
    }];
    
     [NetWorkHelp yl_postDataWith:[NSMutableDictionary dictionaryWithDictionary:@{@"location" : @"gfzmt2"}] url:URL_SPLIT(URL_Interactive, URL_Interactive_Viedo) success:^(id respose) {
        NSLog(@"%@", respose);
        weakSelf.newsImageDataDic = respose[@"body"][@"picOrVideo"];
        NSMutableArray *imagesAry = [NSMutableArray array];
        for (NSDictionary *dic in weakSelf.newsImageDataDic) {
            [imagesAry addObject:dic[@"fileUrl"]];
        }
        weakSelf.newsImageView.imageURLStringsGroup = imagesAry;
        
    } failure:^(NSError *error) {
        
    }];
    
}

@end
